package com.leetcode.LC_MED;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class LC46 {
    public List<List<Integer>> permute(int[] nums) {

        ArrayList<List<Integer>> result = new ArrayList<>();
        HashMap<Integer, Boolean> map = new HashMap<>();
        for (int num : nums) {
            map.put(num, false);
        }
        toAllSort(nums, result, map, new ArrayList<>());
        return result;
    }

    public void toAllSort(int[] nums, List<List<Integer>> result, HashMap<Integer, Boolean> map, List<Integer> list) {
        if (list.size() == nums.length) {
            result.add(new ArrayList<>(list));
            return;
        }
        for (int num : nums) {
            if (!map.get(num)) {
                list.add(num);
                map.put(num, true);
                toAllSort(nums, result, map, list);
                list.remove(list.size() - 1);
                map.put(num, false);
            }
        }
    }
}
